Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
To have the object display an icon, rather than the data, in your application, select the Display As Icon option in the Insert Object dialog box. You’ll still be able to activate the object for editing by double-clicking on the icon, but you won’t be able to view the data in the Visual Basic application. If you don’t need to view the data, this choice can speed up screen display. If you don’t want to insert an object now, click on the Cancel button in the Insert Object dialog box, and Visual Basic will place an empty OLE control on your form. You can still insert an object later during program design. To do so, right-click on the OLE control and select Insert Object from the menu that is displayed. The Insert Object dialog box will be displayed, and you can proceed as described earlier. TIP: Restricting OLE Type An OLE control can hold either a linked or an embedded object. If you want to restrict it to one type or the other, set the OleTypeAllowed property to Linked or Embedded (the default setting is Either). The setting of this property affects object insertion during program design and at runtime. #### Using Paste Special During Program Design Another option during program design is to place an object in an OLE control using the Paste Special command. This method is necessary when the object you want to link does not correspond to an entire data file. For example, you can create an object from a single paragraph of a word-processing document or a block of cells in a spreadsheet. Start by placing the OLE control on your Visual Basic form and leaving it empty by selecting Cancel when the Insert Object dialog box is displayed. Then follow these steps: 1. Start the server application and select the data that you want to link or embed. 2. In the server application, select Copy from the Edit menu. 3. Switch to Visual Basic. 4. Right-click on the OLE control and select Paste Special from the pop-up menu. The Paste Special dialog box is displayed, as shown in Figure 10.4. If the Paste Special command on the pop-up menu is disabled, the data you copied to the Clipboard is not a valid OLE object. 5. In the Paste Special dialog box, the As list displays the server applications that are associated with the data object on the Clipboard. In most cases, this list will contain only one application. If it displays more than one, it means that two or more servers are registered on your system for the type of data on the Clipboard. Select the one that you want associated with the data object. 6. Select the Paste option to embed the object. Select the Paste Link option to link the object. For certain types of objects, only one of these options will be available. 7. Select the Display As Icon option if desired (this option was explained earlier in the chapter). 8. Select OK. #### Inserting An Object At Runtime If you don’t want the OLE objects in your Visual Basic application to be fixed, they must be inserted while the program is executing. The OLE control is left empty during program design; when the program runs, either the program code or the user will place the desired data object in it. Figure 10.4 Use the Paste Special command to load an OLE Container control with an object from the Clipboard. #### The CreateEmbed And CreateLink Methods To create an embedded object, use the OLE control’s CreateEmbed method. This method allows you to create a new embedded object or one that is based on an existing file. The syntax of CreateEmbed is as follows:
    object.CreateEmbed sourcedoc [class]
The identifier object specifies the name of the OLE control. If you want to embed an object based on an existing file, sourcedoc specifies the file, and class can be omitted (or, if not omitted, it is ignored). If you want to create a blank object of a certain class, the class argument specifies the class of the object, and the sourcedoc argument must be a blank string. Here’s an example: If you have an OLE control named OLE1, the command
    OLE1.CreateEmbed “c:\data\report1996.xls”
creates an embedded object based on the spreadsheet file REPORT1996.XLS in the folder C:\DATA. In contrast, the command
    OLE1.CreateEmbed “”, “Excel.Sheet.6”
creates a new, blank object based on an Excel version 6 workbook. You can use the CreateEmbed method only if the OLE control’s OLETypeAllowed property is set to Embedded or Both. Executing this method will erase any object currently in the OLE container without warning. As you’ll soon see, it is your program’s responsibility to save the existing object. The CreateLink method is used to create a linked object. The syntax is as follows:
    object.CreateLink sourcedoc [,sourceitem]
The required argument sourcedoc specifies the data file to which the object is to be linked. The optional argument sourceitem identifies the link data item within the file. Let’s look at a couple of examples. The statement
    OLE1.CreateLink “Evaluation.Doc”
creates a link to the entire Microsoft Word document Evaluation.Doc. The command
    OLE1.CreateLink “Evaluation.Doc”, “Bookmark6”
creates a link to the text in the same document that is identified by the bookmark named Bookmark6. The CreateLink method can be used only if the OLE control’s OLETypeAllowed property is set to Linked or Both. Successful use of both of these methods requires that the specified data file is present and (when required) that the specified link item is present in the file. If not, an error occurs. Also, the class specified for the CreateEmbed method must be a valid class that is present in the system registry. #### Using A Dialog Box To Insert An Object At Runtime I explained earlier in the chapter how to use the Insert Object dialog box during program design to insert an object in an OLE control. You can display the same dialog box during program execution by executing the OLE object’s InsertObjDlg method. This dialog box permits the user to embed or link a new object of a specified class or an object based on an existing file. If the OLE control’s OLETypeAllowed property is set to Linked or Embedded (rather than to Both ), however, only the specified action will be available in the Insert Object dialog box. #### Using Paste Special At Runtime You can also implement a Paste Special command in your Visual Basic program, permitting the user to paste OLE objects into an OLE control at runtime. Of course, a valid OLE object must be on the Clipboard for this method to work. Then you have two approaches. One is to use the OLE control’s Paste method, which is usually executed in response to a Paste command on the application’s Edit menu. The required steps are as follows: 1. Set the OLE control’s OLETypeAllowed property to either Linked or Embedded, depending on whether you want the object to be linked or embedded. 2. Verify that the control’s PasteOK property is True. If not, it means that the Clipboard does not contain an object that is appropriate for the control’s OLETypeAllowed setting. 3. Execute the control’s Paste method. The second and perhaps easier approach is to execute the OLE control’s PasteSpecialDlg method. Executing this method displays the Paste Special dialog box—the same one that is displayed when you select Paste Special from the OLE control’s pop-up menu during program design. In this dialog box, the user selects from the As list (necessary only if more than one server is listed), chooses between the Paste and Paste Link options (to embed or link, respectively), then selects OK. If the OLE control’s OLETypeAllowed property is set to Linked or Embedded, or if the object on the Clipboard permits only linking or embedding, only one of these options will be available. #### Inserting An Object With Drag-And-Drop You can implement a user-friendly interface that permits an object to be inserted in an OLE control with drag-and-drop. The OLE control’s OLEDropAllowed property must be set to True. The user selects data in the server application, drags it to the Visual Basic application, and drops it on the OLE control. The result is the same as if the user had copied the object to the Clipboard and then executed the OLE control’s Paste method. If the user is dragging an appropriate object, the mouse pointer displays a special drop icon when it is over the OLE control. Not all objects are appropriate for drag-and-drop, however. #### Saving And Retrieving OLE Objects It is the responsibility of the Visual Basic program to save the data in its OLE controls. Neither embedded nor linked OLE objects automatically save themselves. If the user closes a form containing an OLE control, the OLE object’s data is lost. This refers to the actual data with an embedded object; with a linked object, the information defining the link to the file that contains the data. For an OLE object to be available the next time the program is run, the data must be explicitly saved and then reloaded into the OLE control when the program is again executed. Capabilities that are built into the OLE control simplify this task.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.